﻿Imports DevExpress.XtraGrid.Views.Grid
Public Class frmVendorlist
    Dim Cboonload As Boolean = True
    Dim MyConn As New ClsSQLhelper
    Dim Strsql As String
    Dim _Mysql As New ClsSQLhelper
    Dim On_load As Boolean = True
    Dim Primary_Key As Integer = -1
#Region "Function"
    Sub Load_Data()
        Try
            Strsql = "select vendorcompany.*,tblprovince.province_name,tblamphur.amphur_name,tbldistrict.ditrict_name,vendor_type.vendor_type_name from vendorcompany "
            Strsql = Strsql & " left join tbldistrict on  tmppart  = distric_code"
            Strsql = Strsql & " left join tblamphur on amppart = amphur_code "
            Strsql = Strsql & " left join tblprovince on chwpart =province_code "
            Strsql = Strsql & " left join vendor_type on companytype = vendor_type_id"
            Dim DT_Vendor_Computer As New DataTable
            DT_Vendor_Computer = _Mysql.GetMYSQLDataTable(Strsql, "vendorcompany")

            With GridControl1
                .DataSource = DT_Vendor_Computer
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
#End Region

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()

    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        With frmNewVendor
            If .ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
            Load_Data()
        End With
    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        With frmEditVendor
            .Primary_Key = Primary_Key
            If .ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
            Load_Data()
        End With
    End Sub

    Private Sub frmVendorlist_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Data()
    End Sub

    Private Sub BtnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRefresh.Click
        Load_Data()
    End Sub

    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try
            Dim Gv As GridView = sender
            Primary_Key = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("companyid"))

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            If MsgBox("คุณต้องการลบข้อมูล " & Primary_Key & " ใช่หรือไม่", MsgBoxStyle.Question + vbYesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub
            Strsql = "delete from vendorcompany where companyid = " & Primary_Key
            _Mysql.MySQLExecute(Strsql)
            MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการลบข้อมูล")
            Load_Data()
        Catch ex As Exception

        End Try
    End Sub
End Class